local skel = fk.CreateSkill {
  name = "emo__shouyou",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__shouyou"] = "兽友",
  [":emo__shouyou"] = "锁定技，游戏开始时，你首次造成伤害后，你首次进入濒死时，你摸一张牌并随机获得一个<a href='emo__shouyou_href'>动物技能</a>。当你发动动物技能后，你会发出叫声。",
  ["emo__shouyou_href"] = "动物技能：即技能名带有动物名的技能。",

  ["$emo__shouyou1"] = "啊呜呜~",
  ["$emo__shouyou2"] = "啊呜~",
}

local isAnimalSkill = function (sname)
  local list = {"豹","马","熊","虎","龙","蛇","鸩","鸟","猫","骊","蛟","凤","象","犬","狗","狼","禽","兽","牛","鹿","莺","雉","鹰",
  "驹","燕","雀","鸿","狐","鹊","鼠","兔","羊","猴","鸡","猪","蟒","驴","蜂","蚁","虫","鱼","螽","蛛","蝎","鹤","豺","貂","蝙","蝠",
  "龟","蛙","蟹","隼","鸽","鲨","鳄","蠕","蜥","獐","蝉","骡","鸭","鹅","蛐","鳍","鱿","鲸","螟","蛉","蛾","獒","豕","彘","鲲","羚",
  "麒","麟","鹭","獾","蝶","鹬","蚌","猢","狲","猘","鹄","鹣","蟨","狸","萤","蜉","蝣","螳","乌","鸦","狍","鸮","枭"}
  local name = Fk:translate(sname, "zh_CN")
  return table.find(list, function (v)
    return string.find(name, v) ~= nil
  end) ~= nil
end

---@param player ServerPlayer
local function gainAnimalSkill(player)
  local room = player.room
  if player.dead then return end
  player:drawCards(1, skel.name)
  if player.dead then return end
  local skills = room:getTag("emo__shouyou_skills")
  if skills == nil then
    skills = {}
    for _, general_name in ipairs(room.general_pile) do
      local general = Fk.generals[general_name]
      for _, skill_name in ipairs(general:getSkillNameList()) do
        if isAnimalSkill(skill_name) then
          table.insertIfNeed(skills, skill_name)
        end
      end
    end
    room:setTag("emo__shouyou_skills", skills)
  end
  skills = table.filter(skills, function (sname)
    local s = Fk.skills[sname]
    return not player:hasSkill(s, true) and
      (not s:hasTag(Skill.AttachedKingdom) or table.contains(s:getSkeleton().attached_kingdom, player.kingdom))
  end)
  if #skills > 0 then
    room:handleAddLoseSkills(player, table.random(skills))
  end
end

skel:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = function (self, event, target, player, data)
    gainAnimalSkill(player)
  end,
})

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("emo__shouyou_damage") == 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__shouyou_damage", 1)
    gainAnimalSkill(player)
  end,
})

skel:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:getMark("emo__shouyou_dying") == 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "emo__shouyou_dying", 1)
    gainAnimalSkill(player)
  end,
})

skel:addEffect(fk.AfterSkillEffect, {
  can_refresh = function (self, event, target, player, data)
    local skill = data.skill
    return target == player and player:hasShownSkill(skill) and player:hasSkill(skel.name)
    and skill:getSkeleton() and skill:getSkeleton().name ~= skel.name
    and isAnimalSkill(skill:getSkeleton().name)
  end,
  on_refresh = function (self, event, target, player, data)
    player:broadcastSkillInvoke(skel.name)
  end,
})

return skel
